package cn.stylefeng.guns.modular.business.frequency.financecheckdetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.financecheckdetail.entity.FinanceCheckDetail;
import cn.stylefeng.guns.modular.business.frequency.financecheckdetail.mapper.FinanceCheckDetailMapper;
import cn.stylefeng.guns.modular.business.frequency.financecheckdetail.model.param.FinanceCheckDetailParam;
import cn.stylefeng.guns.modular.business.frequency.financecheckdetail.service.FinanceCheckDetailService;
import cn.stylefeng.guns.modular.business.frequency.financecheckdetail.enums.FinanceCheckDetailExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 对账单结算记录明细表表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class FinanceCheckDetailServiceImpl extends ServiceImpl<FinanceCheckDetailMapper, FinanceCheckDetail> implements FinanceCheckDetailService {

    @Override
    public PageResult<FinanceCheckDetail> page(FinanceCheckDetailParam financeCheckDetailParam) {

        // 构造条件
        LambdaQueryWrapper<FinanceCheckDetail> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<FinanceCheckDetail> list(FinanceCheckDetailParam financeCheckDetailParam) {

        // 构造条件
        LambdaQueryWrapper<FinanceCheckDetail> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(FinanceCheckDetailParam financeCheckDetailParam) {

        // 将dto转为实体
        FinanceCheckDetail financeCheckDetail = new FinanceCheckDetail();
        BeanUtil.copyProperties(financeCheckDetailParam, financeCheckDetail);

        this.save(financeCheckDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(FinanceCheckDetailParam financeCheckDetailParam) {
        this.removeById(financeCheckDetailParam.getId());
    }

    @Override
    public void edit(FinanceCheckDetailParam financeCheckDetailParam) {

        // 根据id查询实体
        FinanceCheckDetail financeCheckDetail = this.queryFinanceCheckDetail(financeCheckDetailParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(financeCheckDetailParam, financeCheckDetail);

        this.updateById(financeCheckDetail);
    }

    @Override
    public FinanceCheckDetail detail(FinanceCheckDetailParam financeCheckDetailParam) {
        return this.queryFinanceCheckDetail(financeCheckDetailParam);
    }



    /**
     * 获取对账单结算记录明细表表
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private FinanceCheckDetail queryFinanceCheckDetail(FinanceCheckDetailParam financeCheckDetailParam) {
        FinanceCheckDetail financeCheckDetail = this.getById(financeCheckDetailParam.getId());
        if (ObjectUtil.isEmpty(financeCheckDetail)) {
            throw new ServiceException(FinanceCheckDetailExceptionEnum.NOT_EXISTED);
        }
        return financeCheckDetail;
    }
}